#!/usr/bin/env ts-node
import { crawlVideoTypes, crawlVideoDetails, crawlAllVideoDetails, deleteAllVideos } from '../lib/services/crawler-service';
import { crawlBanners, deleteAllBanners } from '../lib/services/banner-crawler-service';
import { executeCrawler, CrawlerType } from '../lib/services/simple-crawler-service';
import '../lib/config/env-setup';

// 显示帮助信息
function showHelp() {
  console.log(`
数据采集系统 - 命令行接口
用法: npm run simple-crawler [命令] [选项]

可用命令:
  video-types                   采集视频类型
  video-details [页码] [数量]    采集视频详情
  all-videos [起始页] [每页数量] [最大页数] [小时]  采集所有视频详情
  banners                       采集轮播图
  banners-and-videos [小时]      并行采集轮播图和最近视频
  delete-all-videos             删除所有视频数据
  delete-all-banners            删除所有轮播图数据

示例:
  npm run simple-crawler video-types
  npm run simple-crawler video-details 1 20
  npm run simple-crawler all-videos 1 20 10
  npm run simple-crawler all-videos 1 20 10 24
  npm run simple-crawler banners
  npm run simple-crawler banners-and-videos 2
  npm run simple-crawler delete-all-videos
  npm run simple-crawler delete-all-banners
`);
}

// 解析命令行参数
async function main() {
  const args = process.argv.slice(2);
  const command = args[0];
  
  if (!command || command === '-h' || command === '--help') {
    showHelp();
    process.exit(0);
  }
  
  try {
    switch (command) {
      case 'video-types':
        console.log('开始采集视频类型...');
        const result = await executeCrawler(CrawlerType.CRAWL_VIDEO_TYPES);
        console.log(result.message);
        break;
        
      case 'video-details':
        const page = parseInt(args[1], 10) || 1;
        const limit = parseInt(args[2], 10) || 20;
        const hours = args[3] ? parseInt(args[3], 10) : undefined;
        console.log(`开始采集视频详情... (页码: ${page}, 每页: ${limit}${hours ? `, 最近${hours}小时` : ''})`);
        const detailsResult = await executeCrawler(CrawlerType.CRAWL_VIDEO_DETAILS, { page, limit, hours });
        console.log(detailsResult.message);
        break;
        
      case 'all-videos':
        const startPage = parseInt(args[1], 10) || 1;
        const pageLimit = parseInt(args[2], 10) || 20;
        const maxPages = args[3] ? parseInt(args[3], 10) : undefined;
        const allHours = args[4] ? parseInt(args[4], 10) : undefined;
        console.log(`开始采集所有视频详情... (起始页: ${startPage}, 每页: ${pageLimit}${maxPages ? `, 最大页数: ${maxPages}` : ''}${allHours ? `, 最近${allHours}小时` : ''})`);
        const allVideosResult = await executeCrawler(CrawlerType.CRAWL_ALL_VIDEO_DETAILS, { 
          startPage, 
          pageLimit, 
          maxPages, 
          hours: allHours 
        });
        console.log(allVideosResult.message);
        break;
      
      case 'banners':
        console.log('开始采集轮播图数据...');
        const bannersResult = await executeCrawler(CrawlerType.CRAWL_BANNERS);
        console.log(bannersResult.message);
        break;
      
      case 'banners-and-videos':
        const parallelsHours = parseInt(args[1], 10) || 2;
        console.log(`开始并行采集轮播图和最近${parallelsHours}小时视频...`);
        const parallelResult = await executeCrawler(CrawlerType.CRAWL_BANNERS_AND_RECENT_VIDEOS, { hours: parallelsHours });
        console.log(parallelResult.message);
        break;
        
      case 'delete-all-videos':
        const deletedCount = await deleteAllVideos();
        console.log(`成功删除 ${deletedCount} 个视频数据`);
        break;
        
      case 'delete-all-banners':
        const bannersDeletedCount = await deleteAllBanners();
        console.log(`成功删除 ${bannersDeletedCount} 个轮播图数据`);
        break;
        
      default:
        console.error(`错误: 未知命令 "${command}"`);
        showHelp();
        process.exit(1);
    }
    
    setTimeout(() => process.exit(0), 1000);
  } catch (error) {
    console.error('执行命令时出错:', error);
    process.exit(1);
  }
}

main().catch(error => {
  console.error('程序出错:', error);
  process.exit(1);
}); 